package com.graduationproject.examonline.utils;

import com.graduationproject.examonline.exam.entity.Exam;
import com.graduationproject.examonline.exam.mapper.ExamMapper;
import com.graduationproject.examonline.exam.service.ExamService;
import com.graduationproject.examonline.webSocket.server.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class UpdateExamStatus {
    private ExamMapper examMapper;

    private WebSocketServer webSocketServer = new WebSocketServer();

    private static UpdateExamStatus instance = new UpdateExamStatus();

    private UpdateExamStatus() {
        this.start();
    }

    @PostConstruct
    public void start() {
        boolean first = FirstRun.getFirstRun().isFirst();
        if (first) {
            Thread t = new Thread() {
                @Override
                public void run() {
                    super.run();
                    while (true) {
                        try {
                            examMapper = (ExamMapper) SpringContextUtil.getBean("examMapper");
                            List<Exam> examList = examMapper.getExamList();
                            int updateAmount = 0;
                            for (int i = 0; i < examList.size(); i++) {
                                Exam exam = examList.get(i);
                                updateAmount += updateStatus(exam);
                            }
                            if (updateAmount > 0) {
                                webSocketServer.updateStatus();
                            }
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            };
            t.start();
        }
    }

    private int updateStatus(Exam exam) {
        try {
            String preStatus = exam.getExam_status();
            String status = "";
            // onging进行中，notStart未开始，over已结束，cancel已取消
            if (!preStatus.equals("cancel")) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date startTime = sdf.parse(exam.getStart_time());
                Date endTime = sdf.parse(exam.getEnd_time());
                Date current = new Date();
                if (current.getTime() < startTime.getTime()) {
                    status = "notStart";
                } else if (current.getTime() < endTime.getTime()) {
                    status = "ongoing";
                } else if (current.getTime() > endTime.getTime()) {
                    status = "over";
                }
            }
            if (!status.equals(preStatus) && !preStatus.equals("cancel")) {
                Map<String, Object> params = new HashMap<String, Object>();
                params.put("exam_id", exam.getExam_id());
                params.put("exam_status", status);
                int updateAmount = examMapper.updateExamById(params);
                System.out.println(new Date().toString() + " 更新了" + updateAmount + "条");
                return updateAmount;
            } else {
                return 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}
